package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult>page(SetmealPageQueryDTO setmealPageQueryDTO){
        return Result.success(setmealService.pageFind(setmealPageQueryDTO));
    }

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#dto.categoryId")
    public Result add(@RequestBody SetmealDTO dto){
        setmealService.add(dto);
        return Result.success();
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    //这里为什么全部删除：因为key接不了list
    public Result delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        return Result.success(setmealService.findById(id));
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    //这里为什么全部删除：因为修改可能会修改套餐分类，例如：原先categoryid是101，现在传入一个102，我需要现删除101的缓存，再写入102的
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐的起售与停售")
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    //这里为什么全部删除：因为修改没有传入套餐分类的id，而且1和0需要单独处理，1是需要put，0则是evict，这里索性就都删了
    public Result startOrStop(@PathVariable Integer status,Long id){
        setmealService.startOrStop(status,id);
        return Result.success();
    }
}
